﻿using System;
using System.Linq;
using System.Management;

namespace FileStorage.Common
{
    public static class SystemUtil
    {
        public static string getOSName()
        {
            try
            {
                var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem");
                string sCPUSerialNumber = "";
                foreach (ManagementObject mo in searcher.Get().Cast<ManagementObject>())
                {
                    sCPUSerialNumber = mo["Name"].ToString().Trim();//操作系统名字
                    //sCPUSerialNumber = mo["BootDevice"].ToString().Trim();//系统启动分区
                    //sCPUSerialNumber = mo["NumberOfProcesses"].ToString().Trim();//当前运行的进程数
                    //sCPUSerialNumber = mo["SerialNumber"].ToString().Trim();//操作系统序列号
                    //sCPUSerialNumber = mo["OSLanguage"].ToString().Trim();//操作系统的语言
                    //sCPUSerialNumber = mo["Manufacturer"].ToString().Trim();//
                }
                return sCPUSerialNumber.Substring(10, 10);//分割字符串
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                return string.Empty;
            }
        }

        public static long currentTimeMillis()
        {
            return decimal.ToInt64(decimal.Divide(DateTime.Now.Ticks - new DateTime(1970, 1, 1, 8, 0, 0).Ticks, 10000));
        }

        public static string accurateToSecond()
        {
            return $"{getServerTime():yyyy年MM月dd日 HH:mm:ss}";
        }
        public static string accurateToMinute()
        {
            return $"{getServerTime():yyyy年MM月dd日 HH:mm}";
        }
        public static string accurateToDay()
        {
            return $"{getServerTime():yyyy年MM月dd日}";
        }
        public static string accurateToLogName()
        {
            return $"{getServerTime():yyyy_MM_dd}";
        }
        public static DateTime getServerTime()
        {
            return DateTime.Now;
        }

        public static long getMaxSizeByString(string input)
        {
            long r = 0L;
            if (string.IsNullOrEmpty(input))
                return -1L;
            try
            {
                if (input.Length > 1)
                {
                    string value = input.Substring(0, input.Length - 1).Trim();
                    string unit = input.Substring(input.Length - 1).ToLower();
                    if (input.Length > 2 && input.ToLower()[input.Length - 1] == 'b')
                    {
                        unit = input.Substring(input.Length - 2, input.Length - 1).ToLower();
                        value = input.Substring(0, input.Length - 2).Trim();
                    }
                    switch (unit)
                    {
                        case "k":
                            r = Convert.ToInt32(value) * 1024L;
                            return r;
                        case "m":
                            r = Convert.ToInt32(value) * 1048576L;
                            return r;
                        case "g":
                            r = Convert.ToInt32(value) * 1073741824L;
                            return r;
                    }
                    r = Convert.ToInt32(input.Trim());
                }
                else
                {
                    r = Convert.ToInt32(input.Trim());
                }
            }
            catch (Exception e) { }
            return r;
        }

        public static string formatMaxUploadFileSize(long size)
        {
            double result = size;
            string unit = "B";
            if (size <= 0L)
                throw new Exception("设置无效，请联系管理员");
            if (size >= 1024L && size < 1048576L)
            {
                result = size / 1024.0D;
                unit = "KB";
            }
            else if (size >= 1048576L && size < 1073741824L)
            {
                result = size / 1048576.0D;
                unit = "MB";
            }
            else if (size >= 1073741824L)
            {
                result = size / 1.073741824E9D;
                unit = "GB";
            }
            return $"{result:F1} {unit}";
        }

        public static long getMaxRateByString(string input)
        {
            long r = 0L;
            if (string.IsNullOrEmpty(input))
                return -1L;
            try
            {
                if (input.Length > 1)
                {
                    string value = input.Substring(0, input.Length - 1).Trim();
                    string unit = input.Substring(input.Length - 1).ToLower();
                    if (input.Length > 2 && input.ToLower()[input.Length - 1] == 'b')
                    {
                        unit = input.Substring(input.Length - 2, input.Length - 1).ToLower();
                        value = input.Substring(0, input.Length - 2).Trim();
                    }
                    switch (unit)
                    {
                        case "m":
                            r = Convert.ToInt32(value) * 1048576L;
                            return r;
                        case "g":
                            r = Convert.ToInt32(value) * 1073741824L;
                            return r;
                    }
                    r = Convert.ToInt32(input.Trim()) * 1024L;
                }
                else
                {
                    r = Convert.ToInt32(input.Trim()) * 1024L;
                }
            }
            catch (Exception e) { }
            return r;
        }
    }
}
